package top.smallway.lightbandassistant_app.network;

import com.alibaba.fastjson2.JSONArray;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.CookieJar;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

// 这是一个网络请求类，��于从指定URL获取消息数据
public class Posts {
    // 当前页码，默认为1
    private int page;
    // OkHttpClient实例，用于发起HTTP请求
    private OkHttpClient httpClient = new OkHttpClient();
    // 请求的URL
    private final String get_message = "https://bs.smallway.top/get_message";
    private final String add_message = "https://bs.smallway.top/add_message";
    private final String del_message = "https://bs.smallway.top/del_message";
    private JSONArray data;

    // 构造函数，传入一个可清除的CookieJar对象
    public Posts() {
        // 初始化当前页码为1
        this.page = 1;
    }

    public void InitCookie(CookieJar cookieJar) {
        this.httpClient = new OkHttpClient.Builder().cookieJar(cookieJar).build();
    }

    public void FomatPage() {
        this.page = 1;
    }

    public JSONArray getmessages() {
        // 创建一个HttpUrl.Builder对象，用于构建请求的URL
        HttpUrl.Builder urlBuilder = HttpUrl.parse(get_message).newBuilder();
        // 向URL中添加查询参数"page"，值为当前页码
        urlBuilder.addQueryParameter("page", String.valueOf(1));
        // 创建一个新的Call对象，用于发起HTTP请求
        try(Response response=httpClient.newCall(new Request.Builder().url(urlBuilder.build().toString()).build()).execute()) {
            return JSONArray.parseArray(response.body().string());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 如果请求失败，返回null
    }

    // 从指定URL获取消息数据的方法
    public JSONArray getmessage() {
        page++;
        // 更新当前页码为下一页

        // 创建一个HttpUrl.Builder对象，用于构建请求的URL
        HttpUrl.Builder urlBuilder = HttpUrl.parse(get_message).newBuilder();
        // 向URL中添加查询参数"page"，值为当前页码
        urlBuilder.addQueryParameter("page", String.valueOf(page));
        // 创建一个新的Call对象，用于发起HTTP请求
        Call call = httpClient.newCall(new Request.Builder().url(urlBuilder.build().toString()).build());
        // 尝试执行请求
        try {
            // 获取请求的响应
            Response response = call.execute();
            // 如果响应成功
            if (response.isSuccessful()) {
                // 将响应的body转换为字符串并解析为JSONObject对象
                return JSONArray.parseArray(response.body().string());
            }
        } catch (IOException e) {
            // 如果发生异常，抛出运行时异常
            throw new RuntimeException(e);
        }
        // 如果请求失败，返回null
        return null;
    }

    public String Add_message(String title, String message, String key, String token) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse(add_message).newBuilder();
        // 构建请求体
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        formBodyBuilder.add("title", title);
        formBodyBuilder.add("message", message);
        formBodyBuilder.add("key", key);

        // 添加请求头
        Request request = new Request.Builder().url(urlBuilder.build().toString()).post(formBodyBuilder.build()).addHeader("X-CSRFToken", token) // 替换为实际的CSRF Token值
                .build();

        try {
            // 发送请求并获取响应
            Response response = httpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                // 返回响应内容
                return response.body().string();
            } else {
                // 处理错误情况
                throw new IOException("Unexpected code " + response);
            }
        } catch (IOException e) {
            // 输出错误信息
            e.printStackTrace();
            return null;
        }
    }

    public String Del_message(int id) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse(del_message).newBuilder();
        urlBuilder.addQueryParameter("id", String.valueOf(id));

        // 构建请求体
        Request request = new Request.Builder().url(urlBuilder.build().toString()).get().build();

        try {
            // 发送请求并获取响应
            Response response = httpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                // 返回响应内容
                return response.body().string();
            } else {
                // 处理错误情况
                throw new IOException("Unexpected code " + response);
            }
        } catch (IOException e) {
            // 输出错误信息
            e.printStackTrace();
            return null;
        }
    }
}